from flask import Flask, request
from flask_cors import CORS
from dataUtil import mongoConnect,sparkConnect
import json
import time

app = Flask(__name__)
CORS(app, resources={r'/*'}, supports_credentials=True)


@app.route("/get_alerts_antipoor", methods=["POST", "GET"])
def get_alerts_by_rule():
    rulee_id = request.args.get("id")
    print(rulee_id)
    rule = mongoConnect.get_rules_by_ids([rulee_id])
    tag_ids = rule['results'][0]['tags']
    results = sparkConnect.get_group_by_tags(tag_ids)
    alert_data = [
        rule['results'][0]['name'] + "预警消息： current num is " + str(len(results)),
    ]
    # data = [820, 932, 901, 934, 1290, 1330, 1320]
    results = {
        "alerts":alert_data,
        "data": [],
    }
    return json.dumps(results,  ensure_ascii=False)


if __name__ == "__main__":
    import setproctitle
    setproctitle.setproctitle("bdmo-antipoor")
    app.debug = True
    app.run(host='0.0.0.0', port=5010)
